import * as PIXI from '../../../../libs/pixi.4.8.0';
import * as JSZip from "../../../../libs/jszip";
import {fgui} from '../../../../libs/fairygui.1.3.0';
import LZString from "../../utils/LZString";

export function paserUiMiddleware(resource, next) {

    if (resource.extension === 'yyui') {
        let bytes = new Uint8Array(resource.data);
        let content = LZString.decompressFromUint8Array(bytes);
        let zip = new JSZip;
        zip.loadAsync(content).then(function ($zip) {
            let fileNum = 0;
            let totalFileNum = Object.keys($zip.files).length;
            $zip.forEach((relativePath, file) => {
                let extension = file.name.split('.').pop();
                var callback = () => {
                    fileNum++;
                    if (fileNum === totalFileNum) {
                        next();
                    }
                }
                extension === 'fui'
                    ? paserFui(file, relativePath, callback)
                    : paserImage(file, relativePath, callback);
            });
        });
    } else if (resource.extension === 'zip') {
        let zip = new JSZip;
        zip.loadAsync(resource.data).then(function ($zip) {
            let fileNum = 0;
            let totalFileNum = Object.keys($zip.files).length;
            $zip.forEach((relativePath, file) => {
                var callback = () => {
                    fileNum++;
                    if (fileNum === totalFileNum) {
                        next();
                    }
                }
                let extension = file.name.split('.').pop();
                switch (extension) {
                    case 'fui':
                        paserFui(file, relativePath, callback);
                        break;
                    case 'png':
                    case 'jpeg':
                    case 'jpg':
                        paserImage(file, relativePath, callback);
                        break;
                    default:
                        next();
                }
            });
        });
    } else {
        next();
    }
}

function paserFui(file, relativePath, callback) {
    var baseName = file.name.substring(file.name.lastIndexOf('/') + 1, file.name.lastIndexOf('.'));
    file.async('arraybuffer').then((content) => {
        let res = new PIXI.loaders.Resource(baseName, relativePath);
        res.data = content;
        fgui.utils.AssetLoader.addResources({
            [baseName]: res
        });
        callback && callback();
    });
}

function paserImage(file, relativePath, callback) {
    var baseName = file.name.substring(file.name.lastIndexOf('/') + 1, file.name.lastIndexOf('.'));
    file.async('base64').then((content) => {
        base64ToImage(content, (img) => {
            let res = new PIXI.loaders.Resource(baseName, relativePath);
            res.data = img;
            res.texture = new PIXI.Texture(new PIXI.BaseTexture(img));
            fgui.utils.AssetLoader.addResources({
                [baseName]: res
            });
            callback && callback();
        });
    });
}

function base64ToImage(data, onCompleteFunc, onCompleteArgArray = null, onCompleteThisArg = null)
{
    var img = new Image();
    img.src = 'data:image/png;base64,'+ data;
    img.onload = function(e)
    {
        if (onCompleteFunc)
        {
            onCompleteArgArray = onCompleteArgArray == null ? [img] : [img].concat(onCompleteArgArray);
            onCompleteFunc.apply(onCompleteThisArg, onCompleteArgArray)
        }
    };
    return img;
}